package com.zryc.shop.sms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.sms.client.IProductServiceClient;
import com.zryc.shop.sms.entity.FlashPromotionProductRelation;
import com.zryc.shop.sms.service.IFlashPromotionProductRelationService;
import com.zryc.shop.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 商品限时购与商品关系表 前端控制器
 * </p>
 *
 * @author LiYe
 * @since 2023-02-13
 */
@RestController
@RequestMapping("/sms/flashProductRelation")
@Api(tags = "商品限时购与商品关系接口列表")
public class FlashPromotionProductRelationController {
    @Autowired
    private IFlashPromotionProductRelationService relationService;
    @Autowired
    private IProductServiceClient productServiceClient;

    @GetMapping("/list")
    @ApiOperation("分页列表")
    public ResponseResult list(@RequestParam(defaultValue = "1") @ApiParam(value = "页码", required = true) Integer pageNum,
                               @RequestParam(defaultValue = "5") @ApiParam(value = "每页显示条数", required = true) Integer pageSize,
                               @RequestParam @ApiParam(value = "秒杀活动ID", required = true) Long flashPromotionId,
                               @RequestParam @ApiParam(value = "秒杀时间段ID", required = true) Long flashPromotionSessionId) {
        QueryWrapper<FlashPromotionProductRelation> queryWrapper = new QueryWrapper();
        queryWrapper.eq("flash_promotion_id", flashPromotionId);
        queryWrapper.eq("flash_promotion_session_id", flashPromotionSessionId);
        Page<FlashPromotionProductRelation> page = new Page(pageNum, pageSize);
        List<FlashPromotionProductRelation> list = relationService.page(page, queryWrapper).getRecords();
        for (FlashPromotionProductRelation relation : list) {
            relation.setProduct(productServiceClient.getInfo(relation.getProductId()).getData());//使用商品ID就可以查到商品信息
        }
        return new ResponseResult(200, "查询完成", page);
    }

    @PostMapping("/create")
    @ApiOperation("添加秒杀商品")
    public ResponseResult create(@RequestBody List<FlashPromotionProductRelation> relationList) {
        relationService.saveBatch(relationList);
        return new ResponseResult(200, "添加成功");
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("删除秒杀商品")
    public ResponseResult delete(@PathVariable @ApiParam(value = "限时购-商品关系ID", required = true) Long id) {
        relationService.removeById(id);
        return new ResponseResult(200, "删除成功");
    }

    @PostMapping("/update/{id}")
    @ApiOperation("修改秒杀商品信息")
    public ResponseResult update(@PathVariable @ApiParam(value = "限时购-商品关系ID", required = true) Long id,
                                 @RequestBody FlashPromotionProductRelation relation) {
        relation.setId(id);
        relationService.updateById(relation);
        return new ResponseResult(200, "修改成功");
    }


}
